from pathlib import Path
from typing import List, Dict
import sys
import os
from PIL import Image
import time
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from .parser_base import BaseParser
from utils import encode_video
from torchvision.io import read_video

class VideoParser(BaseParser):
    """
    Parser for video files
    """
    type = 'video'
    def __init__(self, file_path: str=None, model=None, out_path: str=None) -> None:
        super().__init__(file_path, model, out_path)
        
    def parse(self) -> List[Dict]:

        #video = read_video(self.file_path)
        
        self.parse_output = []
        file_dict = {}
        file_dict['content'] = None
        file_dict['embedding'] = encode_video(self.model, self.file_path)
        print("embedding: ", type(file_dict['embedding']), file_dict['embedding'].shape)
        file_dict['file_path'] = self.file_path
                #添加权限和修改时间
        file_stat = os.stat(self.file_path)
        permission_mode=file_stat.st_mode  
        file_dict['stmode']= oct(permission_mode)[-3:]
        mtime=file_stat.st_mtime
        modified_time=time.ctime(mtime)
        file_dict['mtime']=modified_time
        self.parse_output.append(file_dict)
        
        return self.parse_output


    def _check_format(self) -> bool:
        f_path: Path = Path(self.file_path)
        return f_path.exists() and f_path.suffix in ['mp4', 'avi', 'mov', 'gif']